package com.company.vmvp.api;

import com.company.vmvp.ui.AppConstant;
import com.dee.components.LogUtils;
import com.dee.components.base.mvp.BasePresenter;
import com.dee.components.util.JsonUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * 作者：kkan on 2017/01/30
 * 当前类注释:
 * 订阅封装
 * <p>
 * ----------------使用例子----------------*
 * <p>
 * HttpManager.getOkHttpUrlService() .compose(RxSchedulers.<HttpRespose<Bean>io_main())
 * .subscribe(new RxSubscriber<Bean>(mContext, "加载框信息,不传不显示") {
 * public void _onNext(Bean data) {
 * //处理返回数据,根据需要返回给页面
 * }
 * public void _onError(int code, String message, boolean issuccess) {
 * //处理异常数据
 *  mView.onErrorSuccess(code, message, issuccess,false);
 * }
 * });
 */
public abstract class RxSubscriber<T> implements Observer<T> {
    private Disposable disposable;
    private BasePresenter basePresenter;
    private boolean showDialog;

    /**
     * 构造方法
     * 无加载弹窗
     * 显示返回提示信息
     */
    public RxSubscriber(BasePresenter basePresenter) {
        this.basePresenter=basePresenter;
        showDialog = false;
    }

    /**
     * 构造方法
     * 不显示返回提示信息
     * 有加载弹窗
     *@param loadmsg loadmsg可为空
     * 具体在RxHttpTipLoadDialog设置
     */
    public RxSubscriber(BasePresenter basePresenter, String loadmsg) {
        this.basePresenter = basePresenter;
        showDialog = false;
        if (this.basePresenter != null) {
            showDialog = true;
            this.basePresenter.showDialog(loadmsg);
        }
    }


    /*抽象方法*/
    public abstract void _onNext(T t);

    /*抽象方法
     * 需要在 onError中同意处理异常 则可以写成
     * @CallSuper
     * public void _onError(int code, String message){
     * //要统一处理的异常
     *  }
     */
    public abstract void onErrorSuccess(int code, String message, boolean issuccess);
//    public abstract void _onError(int code, String message);

    @Override
    public void onSubscribe(Disposable d) {
        disposable = d;
        if(this.basePresenter!=null){
            this.basePresenter.mRxManage.add(d);
        }
    }

    /**
     * 请求数据处理
     */
    @Override
    public void onNext(T response) {
        disposeIt();
        LogUtils.eTag("Observer.onNext", JsonUtils.toJson(response));

        if(response instanceof HttpResult){
            HttpResult httpRespose=(HttpResult)response;
            //服务器返回对应的code码
            if (httpRespose.success()) {
                // 这里拦截一下使用测试
                _onNext(response);
            }

            onErrorSuccess(httpRespose.getCode(), httpRespose.getMsg(),httpRespose.success());
        }else {
            _onNext(response);
            onErrorSuccess(AppConstant.HttpSuccessCode, "",true);
        }


        //服务器返回对应的code码
//        if (response.success()) {
//            // 这里拦截一下使用测试
//            _onNext(response.getResult());
//        }else {
//            dismissDialogOnErr();
//        }
//        onErrorSuccess(response.getCode(), response.getMessage(), response.success());
    }

    /**
     * 异常处理
     */
    @Override
    public void onError(Throwable t) {
        LogUtils.eTag("Observer.java", t.getMessage() + "");
        disposeIt();

        dismissDialogOnErr();

        RxException rxException = new RxException(t);
        int errorCode = rxException.getCode();
        String errorMsg = rxException.getMessage();
        onErrorSuccess(errorCode, errorMsg, false);
    }

    @Override
    public void onComplete() {
        disposeIt();
    }

    private void dismissDialogOnErr(){
        if (this.basePresenter != null) {
            this.basePresenter.dismissDialog();
        }
    }

    /**
     * 销毁disposable
     */
    private void disposeIt() {
        if (this.basePresenter != null && showDialog) {
            this.basePresenter.dismissDialog();
        }
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            disposable = null;
        }
    }

}
